import java.awt.*;
import java.awt.event.KeyEvent;

/**
 * 游戏管理类
 * 
 * @author 烦得很
 */
public class GameManager {
    private GameLevel1 gameLevel1;
    private Player player;
    private EventHandlers eventHandlers;
    private EventHandlers.PlayerEvent playerEvent;
    public static int nowPlayerLevel = -1; // 玩家在地图的社么地方

    public GameManager() {
        gameLevel1 = new GameLevel1();
        player = new Player();
        eventHandlers = new EventHandlers();
        playerEvent = eventHandlers.getPlayerEventInsatance();
    }

    /**
     * 全局按键监听
     */
    public void updateMoveDir() {
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(e -> {
            if (e.getID() == KeyEvent.KEY_PRESSED) {
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_UP:
                        Player.nowMoveDir = Player.moveDir.up;
                        break;
                    case KeyEvent.VK_DOWN:
                        Player.nowMoveDir = Player.moveDir.down;
                        break;
                    case KeyEvent.VK_LEFT:
                        Player.nowMoveDir = Player.moveDir.left;
                        break;
                    case KeyEvent.VK_RIGHT:
                        Player.nowMoveDir = Player.moveDir.right;
                        break;
                    default:
                        break;
                }
            }
            return false;
        });
    }
    /**
     * 玩家事件的具体实现
     * @throws Exception 关卡不存在
     */
    private void playerMoveEvent() throws Exception {
        if (player.isPlayerMove) {
            playerEvent.playerMoveEvent(player.getPosition(), nowPlayerLevel, (e) -> {
                int type = e.type; //事件种类
                int eventId = e.eventId; // 辨别哪个敌人，可能有多个
                switch (nowPlayerLevel) {
                    case 1:
                        if (type == GameLevel1.mapEventAreaAndIndex.playerHouse.ordinal()) {
                            if (eventId == 1) {
                                // TODO:玩家房子事件
                            }
                        } else if (type == GameLevel1.mapEventAreaAndIndex.unfinedHouse.ordinal()) {
                            if (eventId == 1) {
                                // TODO:未定义房子事件
                            }
                        } else if (type == GameLevel1.mapEventAreaAndIndex.npc1House.ordinal()) {
                            if (eventId == 1) {
                                // TODO:npc1房子事件
                            }
                        } else if (type == GameLevel1.mapEventAreaAndIndex.enermy1.ordinal()) {
                            if (eventId == 1) {
                                // TODO:敌人1
                            }
                        } else if (type == GameLevel1.mapEventAreaAndIndex.enermy2.ordinal()) {
                            if (eventId == 1) {
                                // TODO:敌人2
                            }
                        }
                        break;
                }
            });
        }
    }
}